#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

bool cmp(string &str1, string &str2)
{
    if (str1.length() == str2.length())
        return str1 > str2;
    else
        return str1.length() < str2.length();
}

int main()
{
    int n = 1, cnt = 1;
    while (cin >> n)
    {
        cnt = 1;
        getchar();
        vector<string> vs;
        while (cnt <= n)
        {
            string str;
            getline(cin, str);
            if (str == "stop")
                break;
            vs.push_back(str);
            cnt++;
        }
        sort(vs.begin(), vs.end(), cmp);
        for (const auto &st : vs)
        {
            cout << st << endl;
        }
    }
    return 0;
}